package ee.karileet.ui.catalogue.view;

import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Map.Entry;
import java.util.Set;

import com.vaadin.addon.touchkit.ui.NavigationButton;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.addon.touchkit.ui.NavigationButton.NavigationButtonClickEvent;
import com.vaadin.addon.touchkit.ui.NavigationButton.NavigationButtonClickListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Layout;

import ee.karileet.productcatalogue.facade.IClientFacade;
import ee.karileet.productcatalogue.facade.impl.ClientFacade;
import ee.karileet.productcatalogue.model.Group;
import ee.karileet.productcatalogue.model.Manufactor;
import ee.karileet.productcatalogue.model.Product;
import ee.karileet.ui.catalogue.util.Const;
import ee.karileet.ui.catalogue.util.Translations;

@SuppressWarnings("serial")
public class GroupView extends NavigationView {
	private IClientFacade facade = new ClientFacade();
	public GroupView(Group group) {
		ResourceBundle rb = Translations.get(null, Const.TRANSLATIONS);
        setCaption(rb.getString(group.getName()));
        
    	Layout layout = new CssLayout();
        layout.setWidth("100%");
        setContent(layout);
        
        VerticalComponentGroup content = null; 
        NavigationButton button = null;
        HashMap<Manufactor, Set<Product>> products = facade.listProductsByGroupWithManufactor(group.getId());
        
        for (Entry<Manufactor, Set<Product>> e : products.entrySet()) {
        	Manufactor m = (Manufactor) e.getKey();
        	content = new VerticalComponentGroup();
        	content.setCaption(m.getName());
        	for (final Product p : (Set<Product>)e.getValue()) {
    			button = new NavigationButton(p.getName());
    	        button.addClickListener(new NavigationButtonClickListener() {
    	            @Override
    	            public void buttonClick(NavigationButtonClickEvent event) {
    	                getNavigationManager().navigateTo(new ProductView(p));                
    	            }
    	        });
    	        content.addComponent(button);
            }	
        	layout.addComponent(content);
        }
        
    };

}
